package indi.zhifa.recipe.bailan5.coupon.controller.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import indi.zhifa.recipe.bailan.framework.auth.entity.annotations.RequireRole;
import indi.zhifa.recipe.bailan.framework.auth.entity.dto.TokenObject;
import indi.zhifa.recipe.bailan.framework.auth.util.ITokenUtil;
import indi.zhifa.recipe.bailan.framework.common.entity.annotations.ZfRestController;
import indi.zhifa.recipe.bailan5.auth.client.util.AuthConst;
import indi.zhifa.recipe.bailan5.coupon.entity.dto.CouponDto;
import indi.zhifa.recipe.bailan5.coupon.entity.po.CouponEntity;
import indi.zhifa.recipe.bailan5.coupon.entity.po.CouponObjectEntity;
import indi.zhifa.recipe.bailan5.coupon.serive.ICouponObjectService;
import indi.zhifa.recipe.bailan5.coupon.serive.ICouponService;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Api(tags = "CouponApi-消费券接口")
@RequestMapping("/api/coupon")
@Slf4j
@ZfRestController
@RequiredArgsConstructor
public class CouponApi {

    private final ICouponService mCouponService;
    private final ICouponObjectService mCouponObjectService;
    private final ITokenUtil mTokenUtil;

    @RequireRole(roles = "company")
    @Operation(summary = "消费券-创建")
    @PostMapping(value = "")
    CouponEntity createCoupon(@Parameter(description = "消费券配置") @RequestBody CouponDto pCouponDto){
        CouponEntity couponEntity = mCouponService.create(pCouponDto);
        return couponEntity;
    }

    @RequireRole(roles = "company")
    @Operation(summary = "消费券-更新")
    @PutMapping(value = "/{id}")
    CouponEntity editCoupon(
            @Parameter(description = "id") @PathVariable(name = "id") Long id,
            @Parameter(description = "消费券配置") @RequestBody CouponDto pCouponDto){
        CouponEntity couponEntity = mCouponService.edit(id,pCouponDto);
        return couponEntity;
    }


    @Operation(summary = "消费券-分页")
    @GetMapping(value = "/page")
    Page<CouponEntity> pageCoupon(
            @Parameter(description = "当前页") @RequestParam(name = "current")  Integer pCurrent,
            @Parameter(description = "页大小") @RequestParam(name = "size")  Integer pSize,
            @Parameter(description = "公司权限Id") @RequestParam(name = "companyId",required = false)  Long pCompanyAuthId,
            @Parameter(description = "消费券名称") @RequestParam(name = "name",required = false)  String pName){
        Page<CouponEntity> couponEntityPage = mCouponService.page(pCurrent,pSize,pCompanyAuthId,pName);
        return couponEntityPage;
    }

    @RequireRole(roles = "guest")
    @Operation(summary = "消费券-获取")
    @GetMapping(value = "/{id}/seize")
    CouponObjectEntity seizeCoupon(
            @Parameter(description = "消费券Id") @PathVariable(name = "id") Long pCouponId) throws InterruptedException {
        TokenObject tokenObject = mTokenUtil.getTokenObject();
        CouponObjectEntity couponObjectEntity = mCouponObjectService.seizeCouponNew(tokenObject.getId(),pCouponId);
        return couponObjectEntity;
    }

    @Operation(summary = "消费券实体-分页")
    @GetMapping(value = "/object/page")
    Page<CouponObjectEntity> listCouponObject(
            @Parameter(description = "当前页") @RequestParam(name = "current") Integer pCurrent,
            @Parameter(description = "页大小") @RequestParam(name = "size") Integer pSize){
        TokenObject tokenObject = mTokenUtil.getTokenObject();
        Page<CouponObjectEntity> couponObjectEntityPage = mCouponObjectService.page(pCurrent,pSize,tokenObject.getId());
        return couponObjectEntityPage;
    }




}
